package com.nanhang.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanhang.common.util.PageResult;
import com.nanhang.entity.SStock;
import com.nanhang.entity.SStorehouse;
import com.nanhang.mapper.SStockMapper;
import com.nanhang.service.ISStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanhang.service.ISStorehouseService;
import com.nanhang.vo.SStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jsj
 * @since 2025-01-09
 */
@Service
public class SStockServiceImpl extends ServiceImpl<SStockMapper, SStock> implements ISStockService {

    @Autowired
    private ISStorehouseService storehouseService;



    @Override
    public PageResult<SStock> search(Integer page, Integer pageSize, SStockVo vo) {

        QueryWrapper<SStock> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getGoodsName()), "goods_name", vo.getGoodsName());
        wrapper.like(StrUtil.isNotBlank((CharSequence) vo.getGoodsPrice()), "goods_price", vo.getGoodsPrice());
        wrapper.eq(vo.getStorehouseId() != null && vo.getStorehouseId() != 0, "storehouse_id", vo.getStorehouseId());

        Page<SStock> page1 = this.page(new Page<>(page, pageSize), wrapper);

        page1.getRecords().forEach(st -> {
            SStorehouse sStorehouse = storehouseService.getById(st.getStorehouseId());
            st.setStorehouseName(sStorehouse.getStorehouseName());
        });

        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

}
